<?php
/**
 * Created By PhpStorm
 * Author: patrick wu
 * Date: 2020/10/8
 * Time: 9:23
 */
namespace App\Service\Api;

use App\Http\Model\MongoDB\ShopTemplate;
use App\Http\Model\Mysql\CategoryModel;

class CategoryService extends BaseService
{
    /**
     * 获取分类页面模板
     * @return array
     */
    public function categoryShow()
    {
        $where['templateType']  =   ShopTemplate::BASIC_TEMPLATE;
        $where['companyId']     =   '621b9cc5-11b3-4d0e-ad82-981351e25336';
        $where['appId']         =   '530defda-cb12-4d24-b0e8-dbfabb76393b';
        $where['_id']           =   'b45cd65d-1428-45c4-835d-076049721792';

        $info  =   ShopTemplate::where($where)->select(['_id', 'release', 'templateName', 'templateType', 'page', 'pageTitle', 'companyId', 'appId', 'sortOrder'])->first();
        return [true, empty($info) ? [] : $info];
    }

    /**
     * 获取所有有效分类数据
     * @return array
     */
    public function categoryData()
    {
        //查看所有分类
        $all_cat    =   CategoryModel::query()->active()->select(['id', 'parent_id', 'cat_name', 'level', 'cat_icon', 'sorting', 'created_at', 'updated_at', 'unique_id'])
                        ->orderBy('sorting', 'desc')->get()->each(function (&$item) {
                            $item->create_time   =   $item->created_at->toDateTimeString();
                            $item->update_time   =   $item->updated_at->toDateTimeString();
                            unset($item->created_at);
                            unset($item->updated_at);
                        });

        if ($all_cat->isEmpty()) {
            return [true, []];
        }

        //分类无限极排序
        return [true, CategoryModel::generateTree($all_cat->toArray())];
    }

}
